package com.keiou.drink.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.keiou.drink.R;
import com.keiou.drink.api.AbsSubscriber;
import com.keiou.drink.api.exception.ApiException;
import com.keiou.drink.api.remote.Api;
import com.keiou.drink.api.rx.RxHelper;
import com.keiou.drink.glide.GlideUtil;
import com.keiou.drink.response.OrderMsgRes;
import com.keiou.drink.response.ProductListRes;
import com.keiou.drink.response.base.HttpResult;
import com.keiou.drink.ui.activity.base.RxActivity;
import com.keiou.drink.util.CToast;
import com.keiou.drink.util.TDevice;
import com.keiou.drink.vo.OrderDetail;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/10/29 0029.
 */

public class ConfirmActivity extends RxActivity {
    @BindView(R.id.iv_shop)
    ImageView ivShop;
    @BindView(R.id.rb_tab_free)
    RadioButton rbTabFree;
    @BindView(R.id.rb_tab_half)
    RadioButton rbTabHalf;
    @BindView(R.id.rb_tab_all)
    RadioButton rbTabAll;
    @BindView(R.id.rg_tab)
    RadioGroup rgTab;
    @BindView(R.id.iv_minu)
    ImageView ivMinu;
    @BindView(R.id.iv_plus)
    ImageView ivPlus;
    @BindView(R.id.iv_sure)
    ImageView ivSure;
    @BindView(R.id.root)
    RelativeLayout root;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.ly_tab)
    LinearLayout lyTab;
    @BindView(R.id.iv_tip_select)
    ImageView ivTipSelect;

    private ProductListRes.ProductsBean productsBean;
    private List<ProductListRes.ProductsBean.AttrListBean> attrList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_confirm;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        GlideUtil.loadImagefitCrop(this, productsBean.getMainPic(), ivShop);
    }

    @Override
    protected void initData() {
        productsBean = (ProductListRes.ProductsBean) getIntent().getExtras().get("product");
        attrList = (null == productsBean.getAttrList() ? new ArrayList<>() : productsBean.getAttrList());
        if (attrList.isEmpty()) {
            ivTipSelect.setVisibility(View.GONE);
            rgTab.setVisibility(View.GONE);
        } else {
            ivTipSelect.setVisibility(View.VISIBLE);
            rgTab.setVisibility(View.VISIBLE);
            boolean isSelect = false;
            for (int i = 0; i < attrList.size(); i++) {
                ProductListRes.ProductsBean.AttrListBean bean = attrList.get(i);
                String code = bean.getAttrCode();
                if ("1".equals(code)) {
                    rbTabFree.setVisibility(View.VISIBLE);
                    rbTabFree.setChecked(!isSelect);
                } else if ("2".equals(code)) {
                    rbTabHalf.setVisibility(View.VISIBLE);
                    rbTabHalf.setChecked(!isSelect);
                } else if ("3".equals(code)) {
                    rbTabAll.setVisibility(View.VISIBLE);
                    rbTabAll.setChecked(!isSelect);
                }
                isSelect = true;
            }
        }

    }

    @Override
    protected void loadData() {

    }


    @OnClick({R.id.iv_minu, R.id.iv_plus, R.id.iv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_minu:
                updateCount(false);
                break;
            case R.id.iv_plus:
                updateCount(true);
                break;
            case R.id.iv_sure:
                //UIUtil.redirectTo(ConfirmActivity.this, PayActivity.class);
                commitOrder();
                break;
        }
    }

    private void updateCount(boolean plus) {
        String countStr = tvCount.getText().toString();
        int count = Integer.valueOf(countStr);
        count = plus ? count + 1 : count - 1;
        if (count >= 1) {
            tvCount.setText(count + "");
        }
    }

    private void commitOrder() {
        String countStr = tvCount.getText().toString();
        int count = Integer.valueOf(countStr);
        String deviceId = TDevice.getDeviceID(this);
        String productId = productsBean.getId();
        float salesPrice = productsBean.getStandardPrice();
        float amount = productsBean.getStandardPrice() * count;

        String attr = null;
        if (!attrList.isEmpty()) {
            int id = rgTab.getCheckedRadioButtonId();
            switch (id) {
                case R.id.rb_tab_free:
                    attr = "1";
                    break;
                case R.id.rb_tab_half:
                    attr = "2";
                    break;
                case R.id.rb_tab_all:
                    attr = "3";
                    break;

            }
        }

        showLoadingDialog();
        String finalAttr = attr;
        addSubscribe(Api.getInstance().submitOrder(deviceId, productId, String.valueOf(salesPrice), String.valueOf(count), String.valueOf(amount), productsBean.getTemperature(), attr)
                .compose(RxHelper.applySchedulers())
                .subscribe(new AbsSubscriber<HttpResult<OrderMsgRes>>() {
                    @Override
                    public void onNext(HttpResult<OrderMsgRes> result) {
                        hideWaitDialog();
                        OrderMsgRes orderMsgRes = result.getResult();
                        OrderDetail orderDetail = new OrderDetail(deviceId, productId, String.valueOf(salesPrice), String.valueOf(count), String.valueOf(amount), finalAttr, orderMsgRes.getOrderId(), orderMsgRes.getAlipayQr(), orderMsgRes.getWechatPayQr(), productsBean.getMainPic());
                        orderDetail.setName(productsBean.getProductName());

                        /**
                         * 加个为0直接跳转到取餐页面
                         */
                        if(0 == amount){
                            redirectToResult(orderDetail);
                        }else{
                            redirectToPay(orderDetail);
                        }


                    }


                    @Override
                    protected void onError(ApiException ex) {
                        hideWaitDialog();
                        CToast.showShort(ConfirmActivity.this, ex.getMsg());
                    }

                    @Override
                    protected void onIntercept(int code) {
                        onInterceptCode(code);
                    }
                }));

    }




    private void redirectToPay(OrderDetail orderDetail) {
        Intent intent = new Intent(this, PayActivity.class);
        intent.putExtra("orderDetail", orderDetail);
        startActivity(intent);
    }


    private void redirectToResult(OrderDetail orderDetail) {
        Intent intent = new Intent(this, ResultActivity.class);
        intent.putExtra("orderId", orderDetail.getOrderId());
        startActivity(intent);
        finish();

    }
}
